package useless;

import useless.LogicExpression;

public class LogicRule {
    String name;
    LogicExpression premise1;
    LogicExpression premise2;
    LogicExpression conclusion;

    public LogicRule(String name, LogicExpression premise1, LogicExpression premise2, LogicExpression conclusion) {
        this.name = name;
        this.premise1 = premise1;
        this.premise2 = premise2;
        this.conclusion = conclusion;
    }

    public LogicRule(String name, LogicExpression premise1, LogicExpression conclusion) {
        this(name, premise1, null, conclusion);
    }

    public boolean canApply(LogicExpression expr1, LogicExpression expr2) {
        return (premise1.expression.equals(expr1.expression) && (premise2 == null || premise2.expression.equals(expr2.expression)));
    }

    public LogicExpression apply(LogicExpression expr1, LogicExpression expr2) {
        if (canApply(expr1, expr2)) {
            return conclusion;
        }
        return null;
    }

    @Override
    public String toString() {
        return name + ": " + premise1 + (premise2 != null ? ", " + premise2 : "") + " -> " + conclusion;
    }
}
